import cgi
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp

class Game(db.Model)
	owner = db.UserProperty()
	name = db.StringPropert(required=True)
	startingcash = db.FloatPropert(required=True)
	allowlimits = db.BooleanProperty(required=True)
	allowstops = db.BooleanProperty(required=True)
	allowmargins = db.BooleanProperty(required=True)
	allowshorts = db.BooleanProperty(required=True)
	commissionpertrade = db.FloatPropert(required=True)
	creditinterestrate = db.FloatPropert(required=True)
	debtinterestrate = db.FloatPropert(required=True)
	maximumtradingvolume = db.IntegerPropert(required=True)
	

class GameService(webapp.RequestHandler):
	def get(self):
		if self.request.get('action')=='creategame':
			portfoliosettingsguid = self.request.get('')
			self.response.out.write(self.createportfolio(portfoliosettingsguid))
		elif self.request.get('action')=='getuseremailaddress':
			self.response.out.write(self.getUserEmailAddress())
		elif self.request.get('action')=='getusernickname':
			self.response.out.write(self.getUserNickname())
		elif self.request.get('action')=='getloginhtml':
			if self.request.get('redirect'):
				redirect = cgi.decode(self.request.get('redirect'))
				self.response.out.write(self.getloginhtml(redirect))
			else:
				self.response.out.write(self.getloginhtml())
		elif self.request.get('action')=='getlogouthtml':
			self.response.out.write(self.getlogouthtml())
		else:
			self.response.out.write('unknown request')
	
	def creategame():
		game = Game{
			owner = users.get_current_user().email(),
			name = self.request.get('name'),
			startingcash = self.request.get('startingcash') or 10000,
			allowlimits = self.request.get('allowlimits') or True,
			allowstops = self.request.get('allowstops') or True,
			allowmargins = self.request.get('allowmargins') or True,
			allowshorts = self.request.get('allowshorts') or True,
			commissionpertrade = self.request.get('comissionpertrade') or 9.95,
			creditinterestrate = self.request.get('creditinterestrate') or 3.00,
			debtinterestrate = self.request.get('debtinterestrate') or 6.00,
			maximumtradingvolume = self.request.get('maximumtradingvolume') or 100.0
		}
		game.put()
	
	def readgames():
		pass
		
	def updategame():
		pass
		
	def deletegame():
		pass
